package com.ling.pl.security.service;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.BatchUtils;
import com.ling.pl.core.commons.utils.reflect.ReflectUtils;
import com.ling.pl.core.dao.IDao;
import com.ling.pl.core.service.impl.BaseService;
import com.ling.pl.security.dao.DefaultCompanyDao;
import com.ling.pl.security.model.DefaultCompany;
import com.ling.pl.security.model.DefaultCompanyVO;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author bo.wang
 */
public class BaseDefaultCompanyService extends BaseService {

    public static final String BEAN_ID = "defaultCompanyService";

    @Resource
    DefaultCompanyDao defaultCompanyDao;

    public IDao getDao() {
        return defaultCompanyDao;
    }

    public List<DefaultCompanyVO> convertDefaultCompanyToVO(List<DefaultCompany> models) {
        List<DefaultCompanyVO> result = new ArrayList<DefaultCompanyVO>();
        for (DefaultCompany initiation : models) {
            DefaultCompanyVO inparam = convertDefaultCompanyToVO(initiation);
            result.add(inparam);
        }
        return result;
    }

    public DefaultCompanyVO convertDefaultCompanyToVO(DefaultCompany model) {
        DefaultCompanyVO inparam = new DefaultCompanyVO();
        ReflectUtils.copyProperties(model, inparam);
        return inparam;
    }

    public DefaultCompany convertDefaultCompanyVOTo(DefaultCompanyVO vo) {
        DefaultCompany entity = new DefaultCompany();
        ReflectUtils.copyProperties(vo, entity);
        return entity;
    }

    public List<DefaultCompany> convertDefaultCompanyVOTo(List<DefaultCompanyVO> vos) {
        List<DefaultCompany> result = new ArrayList<DefaultCompany>();
        for (DefaultCompanyVO vo : vos) {
            DefaultCompany entity = convertDefaultCompanyVOTo(vo);
            result.add(entity);
        }
        return result;
    }

    public DaoPage findDefaultCompanyVOByParams(Map params, Integer pageIndex,
                                                Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = defaultCompanyDao.findDefaultCompanyByParams(params, pageIndex, pageSize);
        daoPage.setResult(convertDefaultCompanyToVO((List<DefaultCompany>) daoPage.getResult()));
        return daoPage;
    }

    public List<DefaultCompanyVO> findDefaultCompanyVOByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        List<DefaultCompany> temp = defaultCompanyDao.findDefaultCompanyByParams(params);
        return convertDefaultCompanyToVO(temp);
    }

    public DaoPage findDefaultCompanyByParams(Map params, Integer pageIndex,
                                              Integer pageSize) {
        if (params == null) {
            params = new HashMap();
        }
        DaoPage daoPage = defaultCompanyDao.findDefaultCompanyByParams(params, pageIndex, pageSize);
        return daoPage;
    }

    public List<DefaultCompany> findDefaultCompanyByParams(Map params) {
        if (params == null) {
            params = new HashMap();
        }
        List<DefaultCompany> temp = defaultCompanyDao.findDefaultCompanyByParams(params);
        return temp;
    }


    public void saveDefaultCompanyDataListsMap(Map<String, List<DefaultCompanyVO>> dataListsMap) {
        Collection<DefaultCompanyVO> deleteList = BatchUtils.to(DefaultCompanyVO.class).getDeleteEntities(dataListsMap);
        Collection<DefaultCompanyVO> insertList = BatchUtils.to(DefaultCompanyVO.class).getInsertEntities(dataListsMap);
        Collection<DefaultCompanyVO> updateList = BatchUtils.to(DefaultCompanyVO.class).getModifiedEntities(dataListsMap);
        if (updateList != null && updateList.size() > 0) {
            for (DefaultCompanyVO vo : updateList) {
                DefaultCompany entity = convertDefaultCompanyVOTo(vo);
                defaultCompanyDao.update(entity);
            }
//			defaultCompanyDao.updateAll(updateList);
        }
        if (insertList != null && insertList.size() > 0) {
            for (DefaultCompanyVO vo : insertList) {
                DefaultCompany entity = convertDefaultCompanyVOTo(vo);
                defaultCompanyDao.save(entity);
                vo.setId(entity.getId());
            }
//			defaultCompanyDao.saveAll(insertList);
        }
        if (deleteList != null && deleteList.size() > 0) {
            for (DefaultCompanyVO vo : deleteList) {
                DefaultCompany entity = new DefaultCompany();
                entity.setId(vo.getId());
                defaultCompanyDao.remove(entity);
            }
//			defaultCompanyDao.removeAll(deleteList);
        }

    }
}

